#include <mutex>

#include "rule_state.h"

namespace Elements
{

RuleState::RuleState(const RuleState& other)
    : Element(other)
    , m_ruleId(other.m_ruleId)
    , m_stateId(other.m_stateId)
    , m_permissions(other.m_permissions)
{
}

bool RuleState::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<RuleState const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_ruleId == otherElement->m_ruleId
        && m_stateId == otherElement->m_stateId
        && m_permissions == otherElement->m_permissions
    );
}

Common::Json RuleState::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["ruleId"] = m_ruleId;
    result["stateId"] = m_stateId;
    result["permissions"] = unsigned(m_permissions);
    return result;
}

void RuleState::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_ruleId = json["ruleId"].get<unsigned>();
    m_stateId = json["stateId"].get<unsigned>();
    m_permissions = Permissions(json["permissions"].get<unsigned>());
}

Element* RuleState::clone() const
{
    return new RuleState(*this);
}

ElementType RuleState::elementType() const
{
    return ElementType::RuleState;
}

unsigned RuleState::ruleId() const
{
    std::shared_lock locker(m_lock);
    return m_ruleId;
}

void RuleState::setRuleId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_ruleId = value;
}

unsigned RuleState::stateId() const
{
    std::shared_lock locker(m_lock);
    return m_stateId;
}

void RuleState::setStateId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_stateId = value;
}

Permissions RuleState::permissions() const
{
    std::shared_lock locker(m_lock);
    return m_permissions;
}

void RuleState::setPermissions(const Permissions& value)
{
    std::unique_lock locker(m_lock);
    m_permissions = value;
}

} // namespace Elements
